package com.xtourms.client.sys.um;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.WorkerField;

public class WorkerManager extends Composite {

	private static WorkerManagerUiBinder uiBinder = GWT
			.create(WorkerManagerUiBinder.class);

	interface WorkerManagerUiBinder extends UiBinder<Widget, WorkerManager> {
	}

	private JSONArray result;
	private ContentFrame parent;
	@UiField FlexTable resultTable;
	
	public WorkerManager(JSONArray rets, ContentFrame parent) {
		initWidget(uiBinder.createAndBindUi(this));
		this.parent = parent;
		this.result = rets;
		initTableHeader();
		fill(rets);
	}

	private void initTableHeader(){
		int col = 0;
		resultTable.getCellFormatter().setWidth(0, col, "30px");
		resultTable.setText(0, col++, "选项");
		resultTable.getCellFormatter().setWidth(0, col, "40px");
		resultTable.setText(0, col++, "编号");
		resultTable.getCellFormatter().setWidth(0, col, "100px");
		resultTable.setText(0, col++, "姓名");
		resultTable.getCellFormatter().setWidth(0, col, "50px");
		resultTable.setText(0, col++, "用户名");
		resultTable.getCellFormatter().setWidth(0, col, "40px");
		resultTable.setText(0, col++, "部门");
		resultTable.getCellFormatter().setWidth(0, col, "40px");
		resultTable.setText(0, col++, "权限");
		resultTable.getRowFormatter().addStyleName(0, "result_header");
	}
	
	private void fill(JSONArray hotels){
		if(null != hotels && hotels.size() > 0){
			for(int index = 0; index < hotels.size(); index++){
				fillRow(index+1, (JSONObject)hotels.get(index));
			}
		}
	}
	
	private void fillRow(int row, JSONObject hotel) {
		int col = 0;
		RadioButton rb = new RadioButton("worker");
		resultTable.setWidget(row, col++, rb);
		resultTable.setText(row, col++, ((JSONNumber)hotel.get(WorkerField.WORKER_ID)).toString());
		String name = ((JSONString)hotel.get(WorkerField.WORKER_NAME)).stringValue();
		resultTable.setText(row, col++, name);
		resultTable.setText(row, col++, ((JSONString)hotel.get(WorkerField.WORKER_USERNAME)).stringValue());
		resultTable.setText(row, col++, ((JSONString)hotel.get(WorkerField.WORKER_DEP)).stringValue());
		resultTable.setText(row, col++, ((JSONString)hotel.get(WorkerField.WORKER_DROIT)).stringValue());
		
		if(0 == row%2){
			resultTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			resultTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiHandler("add_worker")
	void addHotel(ClickEvent evt){
		parent.appendContent(new WorkerDetailManager(parent,new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String ret) {
				JSONObject workerObj = (JSONObject) JSONParser.parse(ret);
				resultTable.clear();
				result = new JSONArray();
				result.set(0, workerObj);
				initTableHeader();
				fillRow(1, workerObj);
				parent.resumeContent();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("员工服务错误:"+caught.getMessage());
			}
		}));
	}
	
	@UiHandler("modify_worker")
	void modifyHotel(ClickEvent evt){
		JSONObject workerObj = null;
		int index = 0;
		if(null != result && result.size() > 0){
			for(; index < result.size(); index++){
				RadioButton rb = (RadioButton)resultTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					workerObj = (JSONObject)result.get(index);
					break;
				}
			}
		}
		if(null != workerObj){
			final int modifyIndex = index;
			parent.appendContent(new WorkerDetailManager(workerObj, parent, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("员工管理服务错误:"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String ret) {
					JSONObject hotelObj = (JSONObject) JSONParser.parse(ret);
					result.set(modifyIndex, hotelObj);
					fillRow(modifyIndex+1, hotelObj);
					parent.resumeContent();
				}
			}));
		}
	}
	
	@UiHandler("delete_worker")
	void manageRoom(ClickEvent evt){
		
	}
}
